package cn.yo2.irain.log4money.app.msgboard;// default package

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class MessagesBoardDAOImpl extends HibernateDaoSupport implements IMessagesBoardDAO {
    private static final Log log = LogFactory.getLog(MessagesBoardDAOImpl.class);
    //property constants
    public static final String EMAIL = "email";
    public static final String CONTENT = "content";
    public static final String REPLY_CONTENT = "replyContent";

    public void save(MessagesBoard msgBoard) {
        log.debug("saving MessagesBoard instance");
        try {
            getHibernateTemplate().saveOrUpdate(msgBoard);
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }

    public void delete(MessagesBoard msgBoard) {
        log.debug("deleting MessagesBoard instance");
        try {
            getHibernateTemplate().delete(msgBoard);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }

    public MessagesBoard findById(String id) {
        log.debug("getting MessagesBoard instance with id: " + id);
        try {
            MessagesBoard instance = (MessagesBoard) getHibernateTemplate()
                    .get("cn.yo2.irain.log4money.app.msgboard.MessagesBoard", id);
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }

    public List<MessagesBoard> findByEmail(String email) {
        return findByProperty(EMAIL, email);
    }

    public List<MessagesBoard> findByContent(String content) {
        return findByProperty(CONTENT, content);
    }

    public List<MessagesBoard> findByReplyContent(String replyContent) {
        return findByProperty(REPLY_CONTENT, replyContent);
    }

    public List<MessagesBoard> findAll() {
        log.debug("finding all MessagesBoard instances");
        try {
            String queryString = "from MessagesBoard order by crtime desc";
            Query queryObject = getSession().createQuery(queryString);
            return queryObject.list();
        } catch (RuntimeException re) {
            log.error("find all failed", re);
            throw re;
        }
    }

    private List<MessagesBoard> findByProperty(String propertyName, Object value) {
        log.debug("finding MessagesBoard instance with property: " + propertyName
                + ", value: " + value);
        try {
            String queryString = "from MessagesBoard as model where model."
                    + propertyName + "= ?";
            Query queryObject = getSession().createQuery(queryString);
            queryObject.setParameter(0, value);
            return queryObject.list();
        } catch (RuntimeException re) {
            log.error("find by property name failed", re);
            throw re;
        }
    }

}